{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "import sys\n",
    "import time\n",
    "from PIL import Image, ImageDraw\n",
    "from utils import *\n",
    "import matplotlib.pyplot as plt\n",
    "import time\n",
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.nn.functional as F\n",
    "import torch.optim as optim\n",
    "import torch.backends.cudnn as cudnn\n",
    "from torchvision import datasets, transforms\n",
    "from torch.autograd import Variable\n",
    "%matplotlib inline\n",
    "import numpy as np\n",
    "import dataset\n",
    "import random\n",
    "import math\n",
    "import json\n",
    "from region_loss import RegionLoss\n",
    "from models import *\n",
    "import h5py\n",
    "os.environ['CUDA_VISIBLE_DEVICES'] = '3'\n",
    "torch.cuda.manual_seed(int(time.time()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "transform=transforms.Compose([\n",
    "                           transforms.ToTensor(),\n",
    "                           transforms.Normalize(mean = [ 0.5, 0.5, 0.5 ],std = [ 0.25, 0.25, 0.25 ]),\n",
    "                       ])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "metadata": {},
   "outputs": [],
   "source": [
    "a = 'FullChannels()'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {},
   "outputs": [],
   "source": [
    "model = eval(a)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "FullChannels(\n",
       "  (model): Sequential(\n",
       "    (0): Sequential(\n",
       "      (0): Conv2d(3, 3, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=3)\n",
       "      (1): ReLU(inplace)\n",
       "      (2): Conv2d(3, 32, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (3): ReLU(inplace)\n",
       "    )\n",
       "    (1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "    (2): Sequential(\n",
       "      (0): Conv2d(32, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=32)\n",
       "      (1): ReLU(inplace)\n",
       "      (2): Conv2d(32, 64, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (3): ReLU(inplace)\n",
       "    )\n",
       "    (3): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "    (4): Sequential(\n",
       "      (0): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=64)\n",
       "      (1): ReLU(inplace)\n",
       "      (2): Conv2d(64, 128, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (3): ReLU(inplace)\n",
       "    )\n",
       "    (5): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)\n",
       "    (6): Sequential(\n",
       "      (0): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=128)\n",
       "      (1): ReLU(inplace)\n",
       "      (2): Conv2d(128, 256, kernel_size=(1, 1), stride=(1, 1))\n",
       "      (3): ReLU(inplace)\n",
       "    )\n",
       "    (7): Conv2d(256, 10, kernel_size=(1, 1), stride=(1, 1), bias=False)\n",
       "  )\n",
       "  (loss): RegionLoss()\n",
       ")"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "load_net('backup/FullChannels-best.weights', model)\n",
    "model.cuda()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {},
   "outputs": [],
   "source": [
    "count = 0\n",
    "for k, v in model.state_dict().items():\n",
    "    data=v.cpu().numpy()\n",
    "    if count<16 and count%2==0:\n",
    "        minimum=1.0/2**7\n",
    "    else:\n",
    "        minimum=1.0/2**7\n",
    "    for x in np.nditer(data, op_flags=['readwrite']):\n",
    "        if x[...]>1:\n",
    "            x[...]=0.99\n",
    "        if x[...]<-1:\n",
    "            x[...]=-0.99\n",
    "        x[...]=round(x[...]/minimum)*minimum\n",
    "    param = torch.from_numpy(data)  \n",
    "    v.copy_(param)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('test.txt', 'r') as outfile:\n",
    "    lines = json.load(outfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_infos = lines[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.0\n",
      "0.0\n",
      "0.586751405084\n",
      "0.648609279152\n",
      "0.0\n",
      "0.729716846209\n",
      "0.790333130822\n",
      "0.0\n",
      "0.0\n",
      "0.726855127749\n",
      "0.485746682508\n",
      "0.763437563599\n",
      "0.709402900521\n",
      "0.0\n",
      "0.465337456754\n",
      "0.374943396648\n",
      "0.0\n",
      "0.0\n",
      "0.706602283654\n",
      "0.447532426714\n",
      "0.0\n",
      "0.46208895677\n",
      "0.557894340541\n",
      "0.611022630514\n",
      "0.522004533858\n",
      "0.148697317715\n",
      "0.654387027572\n",
      "0.786600404302\n",
      "0.0\n",
      "0.0\n",
      "0.0\n",
      "0.821006002505\n",
      "0.516791521948\n",
      "0.0\n",
      "0.0\n",
      "0.784711352785\n",
      "0.0\n",
      "0.809474299829\n",
      "0.0489317717426\n",
      "0.0\n",
      "0.695279807355\n",
      "0.0\n",
      "0.371345476525\n",
      "0.0\n",
      "0.0855455881237\n",
      "0.0\n",
      "0.467609988444\n",
      "0.0\n",
      "0.430543016841\n",
      "0.457038350658\n",
      "0.678923419239\n",
      "0.694189203491\n",
      "0.786879874638\n",
      "0.663458828319\n",
      "0.248276475678\n",
      "0.638945185994\n",
      "0.613406858553\n",
      "0.805635943209\n",
      "0.877148220414\n",
      "0.482308855772\n",
      "0.319044181409\n",
      "0.696525736478\n",
      "0.545868566102\n",
      "0.587184408345\n",
      "0.559829259953\n",
      "0.620437123757\n",
      "0.0\n",
      "0.578496596266\n",
      "0.762276368929\n",
      "0.841025488648\n",
      "0.815095075051\n",
      "0.831196863493\n",
      "0.370816880896\n",
      "0.500992690181\n",
      "0.682745676915\n",
      "0.266410120315\n",
      "0.746823088687\n",
      "0.0\n",
      "0.894569654048\n",
      "0.696489209491\n",
      "0.657037875577\n",
      "0.0\n",
      "0.868747048896\n",
      "0.693761529945\n",
      "0.77243803568\n",
      "0.0\n",
      "0.861781368117\n",
      "0.69585514267\n",
      "0.724508494112\n",
      "0.697272337654\n",
      "0.0\n",
      "0.29635552005\n",
      "0.0\n",
      "0.158245747997\n",
      "0.792422951757\n",
      "0.688190720502\n",
      "0.686818267435\n",
      "0.805833959145\n",
      "0.0\n",
      "0.817873100079\n",
      "0.727998562137\n",
      "0.705346781654\n",
      "0.342783621749\n",
      "0.73434387768\n",
      "0.0\n",
      "0.0892043174525\n",
      "0.761641370095\n",
      "0.61960752901\n",
      "0.0\n",
      "0.0\n",
      "0.817865905507\n",
      "0.794919883885\n",
      "0.0\n",
      "0.0\n",
      "0.690883249661\n",
      "0.729601811398\n",
      "0.686416369749\n",
      "0.696023466565\n",
      "0.691073652172\n",
      "0.721624412114\n",
      "0.759192936085\n",
      "0.736274050304\n",
      "0.810828640244\n",
      "0.0\n",
      "0.0\n",
      "0.24416510244\n",
      "0.77347518222\n",
      "0.77575958653\n",
      "0.812145440746\n",
      "0.0\n",
      "0.0\n",
      "0.626884658677\n",
      "0.398091232798\n",
      "0.698471561462\n",
      "0.680889055275\n",
      "0.0\n",
      "0.0\n",
      "0.629916789311\n",
      "0.0\n",
      "0.0\n",
      "0.727892742989\n",
      "0.466874948972\n",
      "0.671134709345\n",
      "0.0\n",
      "0.61510269134\n",
      "0.700513818485\n",
      "0.583318631454\n",
      "0.539463084237\n",
      "0.0\n",
      "0.0\n",
      "0.0\n",
      "0.670099442303\n",
      "0.0\n",
      "0.0\n",
      "0.0\n",
      "0.328184868289\n",
      "0.84733180567\n",
      "0.723388403885\n",
      "0.0\n",
      "0.229757143033\n",
      "0.0\n",
      "0.916434907869\n",
      "0.617588661409\n",
      "0.0\n",
      "0.692023423698\n",
      "0.403838295222\n",
      "0.0\n",
      "0.0\n",
      "0.290434639438\n",
      "0.709691951124\n",
      "0.647762750167\n",
      "0.464020476829\n",
      "0.675933502803\n",
      "0.0\n",
      "0.679785208542\n",
      "0.695723091436\n",
      "0.766181902224\n",
      "0.0\n",
      "0.900332007447\n",
      "0.154539195255\n",
      "0.0\n",
      "0.542014520235\n",
      "0.618474182124\n",
      "0.799077444407\n",
      "0.538137952528\n",
      "0.643395055595\n",
      "0.800119767534\n",
      "0.0\n",
      "0.549461158043\n",
      "0.716959914919\n",
      "0.775571993456\n",
      "0.0\n",
      "0.432829537846\n",
      "0.0\n",
      "0.0\n",
      "0.668909707896\n",
      "0.683789043371\n",
      "0.698468201274\n",
      "0.563006158121\n",
      "0.830002856868\n",
      "0.759278080997\n",
      "0.378521863023\n",
      "0.0\n",
      "0.596942243116\n",
      "0.766952863236\n",
      "0.456787053749\n",
      "0.529899973066\n",
      "0.379217274476\n",
      "0.719059757191\n",
      "0.597238746144\n",
      "0.586688771773\n",
      "0.0\n",
      "0.808246154079\n",
      "0.572552029004\n",
      "0.76122384298\n",
      "0.3562653301\n",
      "0.624460746344\n",
      "0.0\n",
      "0.820989740301\n",
      "0.618253032289\n",
      "0.630329483559\n",
      "0.557027972609\n",
      "0.202064375975\n",
      "0.829572676049\n",
      "0.638331665963\n",
      "0.0\n",
      "0.0\n",
      "0.0\n",
      "0.580340933795\n",
      "0.350999225006\n",
      "0.808447793196\n",
      "0.751024246253\n",
      "0.747001175385\n",
      "0.372405192942\n",
      "0.72186597973\n",
      "0.741995478181\n",
      "0.626670309452\n",
      "0.621475534095\n",
      "0.475600419067\n",
      "0.74990895681\n",
      "0.794182526531\n",
      "0.52795673945\n",
      "0.734626047626\n",
      "0.530305857568\n",
      "0.771820398295\n",
      "0.0\n",
      "0.736440894832\n",
      "0.788618309126\n",
      "0.648091953008\n",
      "0.0\n",
      "0.0\n",
      "0.605656841416\n",
      "0.0\n",
      "0.762815439172\n",
      "0.80122833947\n",
      "0.486643889377\n",
      "0.88489627744\n",
      "0.818771837411\n",
      "0.0\n",
      "0.883103011663\n",
      "0.0\n",
      "0.741030682823\n",
      "0.675176974366\n",
      "0.545658394763\n",
      "0.18244684333\n",
      "0.0111908893483\n",
      "0.719783657891\n",
      "0.0\n",
      "0.787859933904\n",
      "0.726980067802\n",
      "0.808228467679\n",
      "0.211818788891\n",
      "0.658608979116\n",
      "0.0\n",
      "0.0\n",
      "0.0\n",
      "0.297524120488\n",
      "0.0\n",
      "0.0\n",
      "0.423530518301\n",
      "0.0\n",
      "0.755356425744\n",
      "0.783375665994\n",
      "0.746618756035\n",
      "0.663142282002\n",
      "0.00210972979239\n",
      "0.682652377349\n",
      "0.716278894517\n",
      "0.0\n",
      "0.618406631842\n",
      "0.737631090852\n",
      "0.736350812596\n",
      "0.551138350917\n",
      "0.00327856227705\n",
      "0.676463075533\n",
      "0.0\n",
      "0.0\n",
      "0.616302159827\n",
      "0.603839705154\n",
      "0.637938953023\n",
      "0.62304543622\n",
      "0.0\n",
      "0.574826681683\n",
      "0.678666237175\n",
      "0.698514847938\n",
      "0.605707825794\n",
      "0.459877703046\n",
      "0.766607051891\n",
      "0.565952958149\n",
      "0.616023633397\n",
      "0.0\n",
      "0.0483803062104\n",
      "0.664547992934\n",
      "0.786872148533\n",
      "0.65500522737\n",
      "0.0\n",
      "0.636010453491\n",
      "0.0\n",
      "0.700733670389\n",
      "0.692868343278\n",
      "0.697138660551\n",
      "0.185165348243\n",
      "0.627715054427\n",
      "0.795778837706\n",
      "0.0\n",
      "0.664535367798\n",
      "0.582831913512\n",
      "0.370741793141\n",
      "0.529158975504\n",
      "0.0\n",
      "0.520380781431\n",
      "0.0\n",
      "0.769505921825\n",
      "0.73493445093\n",
      "0.563823129191\n",
      "0.162533553448\n",
      "0.734251296472\n",
      "0.806859125911\n",
      "0.758457330727\n",
      "0.854448058652\n",
      "0.785734965384\n",
      "0.590788798342\n",
      "0.511829952968\n",
      "0.0242046512147\n",
      "0.654644263121\n",
      "0.684876797327\n",
      "0.816555914712\n",
      "0.800198502298\n",
      "0.221023520592\n",
      "0.0\n",
      "0.0\n",
      "0.606006331528\n",
      "0.76623985097\n",
      "0.0155539121155\n",
      "0.717326515299\n",
      "0.0\n",
      "0.815846567195\n",
      "0.584867315044\n",
      "0.428308767524\n",
      "0.799334714382\n",
      "0.554625851886\n",
      "0.0\n",
      "0.847571070352\n",
      "0.0\n",
      "0.639165571546\n",
      "0.774126404265\n",
      "0.0\n",
      "0.596596581021\n",
      "0.467348208957\n",
      "0.826438914035\n",
      "0.646495758181\n",
      "0.583595111177\n",
      "0.594421844828\n",
      "0.0\n",
      "0.632447478273\n",
      "0.594335212204\n",
      "0.599188088507\n",
      "0.648707651637\n",
      "0.0\n",
      "0.479219223272\n",
      "0.833162706387\n",
      "0.0\n",
      "0.0\n",
      "0.433202100728\n",
      "0.188547271959\n",
      "0.645979609877\n",
      "0.495822995964\n",
      "0.777712788718\n",
      "0.0\n",
      "0.0\n",
      "0.806339842432\n",
      "0.52190289322\n",
      "0.474187805306\n",
      "0.586381085296\n",
      "0.683921878249\n",
      "0.576115661981\n",
      "0.168248660594\n",
      "0.812318267413\n",
      "0.0\n",
      "0.0\n",
      "0.685432734964\n",
      "0.759626891312\n",
      "0.0\n",
      "0.0\n",
      "0.800423877245\n",
      "0.0\n",
      "0.36665079408\n",
      "0.754381483039\n",
      "0.74421787937\n",
      "0.0\n",
      "0.199743739395\n",
      "0.789778353142\n",
      "0.0\n",
      "0.750104435216\n",
      "0.660740286782\n",
      "0.655841193629\n",
      "0.0\n",
      "0.0\n",
      "0.783461358525\n",
      "0.0\n",
      "0.631125173696\n",
      "0.907845159777\n",
      "0.661488221743\n",
      "0.0\n",
      "0.628022633753\n",
      "0.559787579417\n",
      "0.661858575257\n",
      "0.633762847826\n",
      "0.285419663266\n",
      "0.812514856935\n",
      "0.0\n",
      "0.642427443272\n",
      "0.641384190385\n",
      "0.869470711579\n",
      "0.0704474834055\n",
      "0.165422662646\n",
      "0.0\n",
      "0.675259230982\n",
      "0.833295818169\n",
      "0.734300187841\n",
      "0.63794208767\n",
      "0.629838267438\n",
      "0.307299735145\n",
      "0.0\n",
      "0.0\n",
      "0.779667868847\n",
      "0.768976263992\n",
      "0.0\n",
      "0.523997295426\n",
      "0.688555285313\n",
      "0.533726216798\n",
      "0.828969501638\n",
      "0.0\n",
      "0.650056438259\n",
      "0.196235172381\n",
      "0.62079526751\n",
      "0.540339150435\n",
      "0.653347874647\n",
      "0.0\n",
      "0.622476108643\n",
      "0.894379400942\n",
      "0.722020542917\n",
      "0.0\n",
      "0.606750914121\n",
      "0.733181660616\n",
      "0.0\n",
      "0.811114687926\n",
      "0.0\n",
      "0.459391941207\n",
      "0.698818455935\n",
      "0.627947902768\n",
      "0.664449211816\n",
      "0.69254787645\n",
      "0.662089625602\n",
      "0.869943145276\n",
      "0.711043262084\n",
      "0.252924649537\n",
      "0.69684717871\n",
      "0.716590382962\n",
      "0.0\n",
      "0.0\n",
      "0.693979071987\n",
      "0.479547144841\n",
      "0.0\n",
      "0.635349344711\n",
      "0.0\n",
      "0.205795763916\n",
      "0.763022872333\n",
      "0.810966675157\n",
      "0.488910283003\n",
      "0.0\n",
      "0.580593450008\n",
      "0.613138719037\n",
      "0.132432514616\n",
      "0.749747074709\n",
      "0.788066245503\n",
      "0.583082076448\n",
      "0.654352620456\n",
      "0.704792027216\n",
      "0.809444688808\n",
      "0.0\n",
      "0.775784032308\n",
      "0.606384851563\n",
      "0.0\n",
      "0.647674941946\n",
      "0.0\n",
      "0.472630294546\n",
      "0.737251703164\n",
      "0.673900257561\n",
      "0.588614067957\n",
      "0.0\n",
      "0.378421329489\n",
      "0.666577414215\n",
      "0.590305021152\n",
      "0.778537045494\n",
      "0.0\n",
      "0.865288768163\n",
      "0.0\n",
      "0.0\n",
      "0.0\n",
      "0.0\n",
      "0.0693619300331\n",
      "0.705501731011\n",
      "0.0\n",
      "0.91606482592\n",
      "0.755170736322\n",
      "0.0\n",
      "0.615814617406\n",
      "0.0971777448268\n",
      "0.0\n",
      "0.699845811675\n",
      "0.679843726086\n",
      "0.540797384324\n",
      "0.63795095701\n",
      "0.0899655169962\n",
      "0.683967303218\n",
      "0.0\n",
      "0.601154759877\n",
      "0.672809855734\n",
      "0.617220208164\n",
      "0.389066985655\n",
      "0.0\n",
      "0.771293958767\n",
      "0.0\n",
      "0.384204972729\n",
      "0.717275548815\n",
      "0.0\n",
      "0.561261450699\n",
      "0.736024295734\n",
      "0.0\n",
      "0.689982833542\n",
      "0.859717026543\n",
      "0.0\n",
      "0.709888299431\n",
      "0.0\n",
      "0.376746719607\n",
      "0.680729704926\n",
      "0.0\n",
      "0.583410035368\n",
      "0.0\n",
      "0.487465667471\n",
      "0.687510302248\n",
      "0.0264863812075\n",
      "0.676001714346\n",
      "0.681803965162\n",
      "0.874014791552\n",
      "0.0\n",
      "0.791071389906\n",
      "0.566953821679\n",
      "0.795043760454\n",
      "0.0\n",
      "0.751341787929\n",
      "0.170645542519\n",
      "0.714778701653\n",
      "0.53378054211\n",
      "0.612935061127\n",
      "0.560412398261\n",
      "0.575462200618\n",
      "0.767770959234\n",
      "0.543014796558\n",
      "0.700618272746\n",
      "0.687402046686\n",
      "0.761980952425\n",
      "0.760858056254\n",
      "0.739407197043\n",
      "0.899982353052\n",
      "0.0\n",
      "0.708785210201\n",
      "0.0\n",
      "0.560382308107\n",
      "0.884631107227\n",
      "0.735164159223\n",
      "0.492242428368\n",
      "0.838995815172\n",
      "0.0\n",
      "0.0\n",
      "0.615791367379\n",
      "0.827615693231\n",
      "0.846505440472\n",
      "0.0\n",
      "0.721125873313\n",
      "0.594048813451\n",
      "0.805843031665\n",
      "0.655608005687\n",
      "0.0\n",
      "0.698424743017\n",
      "0.0\n",
      "0.0\n",
      "0.0\n",
      "0.523461024192\n",
      "0.0\n",
      "0.55990872735\n",
      "0.0286009658372\n",
      "0.585415623805\n",
      "0.591456998225\n",
      "0.0\n",
      "0.568823074845\n",
      "0.832416077927\n",
      "0.0\n",
      "0.0\n",
      "0.765729644248\n",
      "0.343085263955\n",
      "0.547342530657\n",
      "0.671615006503\n",
      "0.688397637434\n",
      "0.783842671419\n",
      "0.0\n",
      "0.648708578691\n",
      "0.724058438946\n",
      "0.653579346956\n",
      "0.774644421837\n",
      "0.272563351179\n",
      "0.651508345486\n",
      "0.818436515031\n",
      "0.320211483082\n",
      "0.0\n",
      "0.0\n",
      "0.653775902943\n",
      "0.76920021085\n",
      "0.0\n",
      "0.708307562586\n",
      "0.581123806237\n",
      "0.0\n",
      "0.73831820513\n",
      "0.590890515346\n",
      "0.75460769524\n",
      "0.0\n",
      "0.0\n",
      "0.78939164646\n",
      "0.65249703275\n",
      "0.0280803966677\n",
      "0.779189895172\n",
      "0.4780239665\n",
      "0.65195749098\n",
      "0.207095945476\n",
      "0.754117138825\n",
      "0.709373779497\n",
      "0.624457059311\n",
      "0.421520722874\n",
      "0.0\n",
      "0.819652628223\n",
      "0.465173412796\n",
      "0.686734376518\n",
      "0.49079635554\n",
      "0.447731731512\n",
      "0.0\n",
      "0.662858986698\n",
      "0.783233575081\n",
      "0.551287202189\n",
      "0.0\n",
      "0.479457551476\n",
      "0.0\n",
      "0.0\n",
      "0.777220280299\n",
      "0.723255913204\n",
      "0.0\n",
      "0.544922999865\n",
      "0.685554928547\n",
      "0.782139203962\n",
      "0.897907875362\n",
      "0.0\n",
      "0.0\n",
      "0.605665014026\n",
      "0.0\n",
      "0.0\n",
      "0.48355847545\n",
      "0.157277264731\n",
      "0.0\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0.656543083878\n",
      "0.0\n",
      "0.717411300508\n",
      "0.843359471582\n",
      "0.300615746303\n",
      "0.0\n",
      "0.656953483393\n",
      "0.237646962448\n",
      "0.0\n",
      "0.823365109644\n",
      "0.633692991335\n",
      "0.888947187362\n",
      "0.545488330017\n",
      "0.721596378714\n",
      "0.556831852795\n",
      "0.844785806958\n",
      "0.535919471196\n",
      "0.726672203761\n",
      "0.589746773894\n",
      "0.769678346577\n",
      "0.399814993142\n",
      "0.600162207146\n",
      "0.55041723813\n",
      "0.758776514589\n",
      "0.801502256217\n",
      "0.640173487524\n",
      "0.0\n",
      "0.459354720379\n",
      "0.74625234239\n",
      "0.883232215037\n",
      "0.671005396412\n",
      "0.72725257641\n",
      "0.0\n",
      "0.302012743143\n",
      "0.819718670613\n",
      "0.248175510721\n",
      "0.0\n",
      "0.46162971124\n",
      "0.746835392564\n",
      "0.0\n",
      "0.662153931576\n",
      "0.811792326085\n",
      "0.0\n",
      "0.806366168063\n",
      "0.0\n",
      "0.822106141087\n",
      "0.0\n",
      "0.0\n",
      "0.71937644076\n",
      "0.572588473415\n",
      "0.0\n",
      "0.830277726549\n",
      "0.590032073939\n",
      "0.540219542438\n",
      "0.698538465013\n",
      "0.23198834682\n",
      "0.803427943976\n",
      "0.579022585336\n",
      "0.77906145051\n",
      "0.666743550298\n",
      "0.603798448416\n",
      "0.444475565587\n",
      "0.768152787645\n",
      "0.717031346619\n",
      "0.848575494171\n",
      "0.74917725201\n",
      "0.872749642827\n",
      "0.53948232872\n",
      "0.0551312556655\n",
      "0.0\n",
      "0.645040019013\n",
      "0.0\n",
      "0.414200364562\n",
      "0.68113245476\n",
      "0.66268693423\n",
      "0.75078001011\n",
      "0.611491142269\n",
      "0.681268652119\n",
      "0.747679630319\n",
      "0.732216037816\n",
      "0.674305113835\n",
      "0.0\n",
      "0.863177522546\n",
      "0.0\n",
      "0.594775325561\n",
      "0.682921130425\n",
      "0.771572715536\n",
      "0.63876021809\n",
      "0.816971747803\n",
      "0.14917911008\n",
      "0.0\n",
      "0.712303800691\n",
      "0.0\n",
      "0.0\n",
      "0.574152943631\n",
      "0.0\n",
      "0.0\n",
      "0.0130652343279\n",
      "0.450760800937\n",
      "0.691175666533\n",
      "0.0\n",
      "0.0\n",
      "0.757779875362\n",
      "0.450781656646\n",
      "0.770629857535\n",
      "0.487054614823\n",
      "0.782535553289\n",
      "0.722832258765\n",
      "0.0\n",
      "0.669167400725\n",
      "0.712947736793\n",
      "0.0\n",
      "0.607197136995\n",
      "0.0\n",
      "0.26487189209\n",
      "0.746639931728\n",
      "0.639217694503\n",
      "0.876973622472\n",
      "0.720815003897\n",
      "0.711360829731\n",
      "0.623410492874\n",
      "0.799974259937\n",
      "0.553194650016\n",
      "0.722192923532\n",
      "0.831046737402\n",
      "0.0\n",
      "0.527906581349\n",
      "0.0\n",
      "0.0\n",
      "0.768016200313\n",
      "0.680809731197\n",
      "0.784230106739\n",
      "0.0\n",
      "0.915330736677\n",
      "0.786352703907\n",
      "0.661341135154\n",
      "0.765700807392\n",
      "0.0491157850612\n",
      "0.624127261238\n",
      "0.635463600471\n",
      "0.721974887812\n",
      "0.819602554985\n",
      "0.823030319604\n",
      "0.0\n",
      "0.0\n",
      "0.703716999538\n",
      "0.821362417618\n",
      "0.667222685277\n",
      "0.713696816976\n",
      "0.63312418679\n",
      "0.740379874937\n",
      "0.537377400922\n",
      "0.715592812517\n",
      "0.143567232756\n",
      "0.712862658264\n",
      "0.753083432653\n",
      "0.608852047794\n",
      "0.0\n",
      "0.594861584875\n",
      "0.656054696353\n",
      "0.0\n",
      "0.0\n",
      "0.731853633965\n",
      "0.414118032627\n",
      "0.0\n",
      "0.0\n",
      "0.235088947846\n",
      "0.654316414739\n",
      "0.0\n",
      "0.0\n",
      "0.867032938509\n",
      "0.605679948539\n",
      "0.421754632477\n",
      "0.556889822564\n",
      "0.0\n",
      "0.649610987843\n",
      "0.512693954582\n",
      "0.712715585559\n",
      "0.00487983480314\n",
      "0.67722934968\n",
      "0.528106010055\n",
      "0.625917939736\n",
      "0.22734851308\n",
      "0.0\n",
      "0.707283555248\n",
      "0.0\n",
      "0.575904508481\n",
      "0.672359904128\n",
      "0.0\n",
      "0.763903747464\n",
      "0.361014780374\n",
      "0.0\n",
      "0.682563810269\n",
      "0.0\n",
      "0.0\n",
      "0.429593640771\n",
      "0.790227162546\n",
      "0.0\n",
      "0.485548635652\n",
      "0.849778803783\n",
      "0.0\n",
      "0.72488244366\n",
      "0.736812772441\n",
      "0.754008571945\n",
      "0.44679580581\n",
      "0.590971590281\n",
      "0.0\n",
      "0.269042791022\n",
      "0.799444036472\n",
      "0.0\n",
      "0.705669965954\n",
      "0.458822111289\n",
      "0.0\n",
      "0.709626370525\n",
      "0.678246848095\n",
      "0.0\n",
      "0.0\n",
      "0.686384050336\n",
      "0.54834920258\n",
      "0.548538582469\n",
      "0.810089009248\n",
      "0.000721642946171\n",
      "0.512103153754\n",
      "0.0\n",
      "0.685242179843\n",
      "0.0\n",
      "0.862199163124\n",
      "0.587110407029\n",
      "0.83042865699\n",
      "0.621830144319\n",
      "0.219831025009\n",
      "0.756830658653\n",
      "0.818976414208\n",
      "0.797357264645\n",
      "0.455142273333\n",
      "0.0\n",
      "0.739984321887\n",
      "0.75447871924\n",
      "0.76587567406\n",
      "0.874519613026\n",
      "0.243953810279\n",
      "0.0\n",
      "0.635579400934\n",
      "0.406738894123\n",
      "0.430298135146\n",
      "0.757411157471\n",
      "0.430982787712\n",
      "0.574726611901\n",
      "0.447606863089\n",
      "0.0\n",
      "0.0\n",
      "0.832182783541\n",
      "0.0237349566484\n",
      "0.353912002061\n",
      "0.0\n",
      "0.675755298896\n",
      "0.31200584145\n",
      "0.814761462645\n",
      "0.690112440239\n",
      "0.0\n",
      "0.845551215028\n",
      "0.384571183272\n",
      "0.0\n",
      "0.509438974453\n",
      "0.0\n",
      "0.0\n",
      "0.777188129642\n",
      "0.0\n",
      "0.560335343971\n",
      "0.0\n",
      "0.745248844063\n",
      "0.165074640754\n",
      "0.0\n",
      "0.657192913075\n",
      "0.615849060883\n",
      "0.542369192634\n",
      "0.0\n",
      "0.0198191363643\n",
      "0.614583715825\n",
      "0.270076349094\n",
      "0.698847517255\n",
      "0.826747904455\n",
      "0.46773647667\n",
      "0.792321738608\n",
      "0.580916601637\n",
      "0.777148859117\n",
      "0.754462380624\n",
      "0.85391608526\n",
      "0.438710814147\n",
      "0.676033670737\n",
      "0.503561963688\n",
      "0.439857281043\n",
      "0.551160105569\n",
      "0.0\n",
      "0.596211887941\n",
      "0.411358020144\n",
      "0.0214240352754\n",
      "0.667318219314\n",
      "0.0\n",
      "0.0\n",
      "0.674730287215\n",
      "0.0\n",
      "0.0\n",
      "0.0\n",
      "0.736806876156\n",
      "0.0\n",
      "0.0\n",
      "0.0\n"
     ]
    }
   ],
   "source": [
    "pred_infos = []\n",
    "anchors     = model.anchors\n",
    "num_anchors = model.num_anchors\n",
    "anchor_step = len(anchors)/num_anchors\n",
    "proposals = 0.0\n",
    "total = 0.0\n",
    "for i in range(len(test_infos)):\n",
    "    imgpath = test_infos[i][0]\n",
    "    box = test_infos[i][1]\n",
    "    label = np.zeros(4)\n",
    "    label[0:2] = box[2:4]\n",
    "    label[2:4] = box[0:2]\n",
    "    img = Image.open(imgpath).convert('RGB')\n",
    "    img = img.resize((model.width, model.height))\n",
    "    timg = transform(img)\n",
    "    timg = timg.view(1, 3, model.height, model.width)\n",
    "    output = model(timg.cuda()).data\n",
    "    batch = output.size(0)\n",
    "    h = output.size(2)\n",
    "    w = output.size(3)\n",
    "    output = output.view(batch*num_anchors, 5, h*w).transpose(0,1).contiguous().view(5, batch*num_anchors*h*w)\n",
    "    grid_x = torch.linspace(0, w-1, w).repeat(h,1).repeat(batch*num_anchors, 1, 1).view(batch*num_anchors*h*w).cuda()\n",
    "    grid_y = torch.linspace(0, h-1, h).repeat(w,1).t().repeat(batch*num_anchors, 1, 1).view(batch*num_anchors*h*w).cuda()\n",
    "    xs = torch.sigmoid(output[0]) + grid_x\n",
    "    ys = torch.sigmoid(output[1]) + grid_y\n",
    "\n",
    "    anchor_w = torch.Tensor(anchors).view(num_anchors, anchor_step).index_select(1, torch.LongTensor([0]))\n",
    "    anchor_h = torch.Tensor(anchors).view(num_anchors, anchor_step).index_select(1, torch.LongTensor([1]))\n",
    "    anchor_w = anchor_w.repeat(batch, 1).repeat(1, 1, h*w).view(batch*num_anchors*h*w).cuda()\n",
    "    anchor_h = anchor_h.repeat(batch, 1).repeat(1, 1, h*w).view(batch*num_anchors*h*w).cuda()\n",
    "    ws = torch.exp(output[2]) * anchor_w\n",
    "    hs = torch.exp(output[3]) * anchor_h\n",
    "    det_confs = torch.sigmoid(output[4])\n",
    "    sz_hw = h*w\n",
    "    sz_hwa = sz_hw*num_anchors\n",
    "    det_confs = convert2cpu(det_confs)\n",
    "    xs = convert2cpu(xs)\n",
    "    ys = convert2cpu(ys)\n",
    "    ws = convert2cpu(ws)\n",
    "    hs = convert2cpu(hs)        \n",
    "\n",
    "    for b in range(batch):\n",
    "        det_confs_inb = det_confs[b*sz_hwa:(b+1)*sz_hwa].numpy()\n",
    "        xs_inb = xs[b*sz_hwa:(b+1)*sz_hwa].numpy()\n",
    "        ys_inb = ys[b*sz_hwa:(b+1)*sz_hwa].numpy()\n",
    "        ws_inb = ws[b*sz_hwa:(b+1)*sz_hwa].numpy()\n",
    "        hs_inb = hs[b*sz_hwa:(b+1)*sz_hwa].numpy()      \n",
    "        ind = np.argmax(det_confs_inb)\n",
    "\n",
    "        bcx = xs_inb[ind]\n",
    "        bcy = ys_inb[ind]\n",
    "        bw = ws_inb[ind]\n",
    "        bh = hs_inb[ind]\n",
    "\n",
    "        box = [bcx/w, bcy/h, bw/w, bh/h]\n",
    "\n",
    "        iou = bbox_iou(box, label, x1y1x2y2=False)\n",
    "        print (iou)\n",
    "        \n",
    "        pred_infos.append([imgpath,box])\n",
    "        proposals = proposals + iou\n",
    "        total = total+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.45871142181457863"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "proposals/total"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {},
   "outputs": [],
   "source": [
    "with open('pred_infos.json', 'w') as outfile:\n",
    "    json.dump(pred_infos, outfile)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {},
   "outputs": [],
   "source": [
    "param_list=[]\n",
    "for key, value in model.state_dict().items():\n",
    "    flat_weight = value.contiguous().view(value.numel())\n",
    "    param_list.extend(flat_weight.tolist())\n",
    "for i in param_list:\n",
    "    i = float(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "48414"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(param_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "193656"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import struct\n",
    "fp = open(\"param_v19best.bin\",'wb')\n",
    "s = struct.pack('f'*len(param_list), *param_list)\n",
    "fp.write(s)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(320, 176)"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "img.size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
